Ext.extend(app.module,{
    currentFormValues: {},
    init: function(){
        mainPanel = this;
        //用于列表
        this.cgcxDs = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'fruit/list.do'}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'},
                {name:'dept'},
                {name:'count'},
                {name:'rank'},
                {name:'state'},
                {name:'time'},
                {name:'userid'},
                {name:'fruittype'},
                {name:'username'},
                {name:'fruitTypeName'}
            ])
        });

        var cgcx_pagesize_combo = new Ext.form.ComboBox( {       //设置分页栏的每页记录数选择
            name : 'pagesize',
            hiddenName : 'pagesize',
            typeAhead : true,
            triggerAction : 'all',
            lazyRender : true,
            mode : 'local',
            store : new Ext.data.ArrayStore(
                {
                    fields : [ 'value', 'text' ],
                    data : [ [ 20, '20条/页' ], [ 50, '50条/页' ],
                        [ 100, '100条/页' ], [ 200, '200条/页' ]]
                }
            ),
            valueField : 'value',
            displayField : 'text',
            value : '20',
            editable : false,
            width : 85
        });
        var number = parseInt(cgcx_pagesize_combo.getValue());

        cgcx_pagesize_combo.on("select", function(comboBox) {
            var bbar = Ext.getCmp('cgcxbbar');
            bbar.pageSize = parseInt(comboBox.getValue());
            number = parseInt(comboBox.getValue());
            mainPanel.ds.reload( {
                params : {
                    start : 0,
                    limit : bbar.pageSize
                }
            });
        });

        var fruitTypeForCgcxStore = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'fruittype/list.do'}),
            reader: new Ext.data.JsonReader({
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'}
            ])
        });
        var comboProjectTypeForCgcx = new Ext.form.ComboBox({
            store: fruitTypeForCgcxStore,
            editable: false,
            emptyText: '成果类型...',
            mode: 'remote',
            hiddenName : 'fruittypeid',
            typeAhead : true,
            triggerAction : 'all',
            displayField: 'name',
            valueField: 'id',
            width:100,
            id:'cgcxQueryFruitType'
        });

        this.body = new Ext.grid.GridPanel({
            store: this.cgcxDs,
            region: 'center',
            loadMask: true,
            viewConfig:{
                forceFit:true
            },
            columns: [
                new Ext.grid.RowNumberer(),
                {
                    xtype: 'gridcolumn',
                    dataIndex: 'id',
                    header: 'id',
                    hidden: true
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'username',
                    header: '姓名'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'name',
                    header: '成果名称'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'fruitTypeName',
                    header: '成果种类'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'dept',
                    header: '成果来源'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'time',
                    header: '取得时间',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        return Ext.util.Format.date(new Date(parseInt(value)), 'Y-m');
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'count',
                    header: '合作排名',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        var rank = record.data['rank'];
                        if(rank==1 && value==1){
                            return '独撰';
                        } else {
                            return rank+'/'+count;
                        }
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'note',
                    header: '备注'
                }
            ],
            tbar: {
                xtype: 'toolbar',
                items: [
                    {
                        xtype: 'displayfield',
                        value: '查询条件：'
                    },
                    {
                        xtype: 'displayfield',
                        value: '取得时间（日期范围）：'
                    },
                    {
                        xtype: 'datefield',
                        name: 'startdt',
                        id: 'cgcxstartdt',
                        vtype: 'daterange',
                        endDateField: 'cgcxenddt',
                        emptyText: '开始',
                        width: 110,
                        format:'Y-m-d'
                    },
                    {
                        xtype: 'datefield',
                        name: 'enddt',
                        id: 'cgcxenddt',
                        vtype: 'daterange',
                        startDateField: 'cgcxstartdt',
                        emptyText: '结束',
                        width: 110,
                        format:'Y-m-d'
                    },
                    comboProjectTypeForCgcx,
                    {
                        xtype: 'textfield',
                        id : 'cgcxQueryUserName',
                        name : 'cgcxQueryUserName',
                        emptyText : '姓名',
                        enableKeyEvents : true,
                        listeners : {
                            specialkey : function(field, e) {
                                if (e.getKey() == Ext.EventObject.ENTER) {
                                    mainPanel.query();
                                }
                            }
                        },
                        width : 100
                    },
                    {
                        xtype: 'textfield',
                        id : 'cgcxQueryName',
                        name : 'cgcxQueryName',
                        emptyText : '成果名称',
                        enableKeyEvents : true,
                        listeners : {
                            specialkey : function(field, e) {
                                if (e.getKey() == Ext.EventObject.ENTER) {
                                    mainPanel.query();
                                }
                            }
                        },
                        width : 100
                    },
                    {
                        text : '查询',
                        iconCls : 'btn-search',
                        handler : function() {
                            mainPanel.query();
                        }
                    }

                ]
            },
            bbar: {
                id: 'cgcxbbar',
                xtype: 'paging',
                pageSize : number,
                store : this.cgcxDs,
                displayInfo : true,
                displayMsg : '显示{0}条到{1}条,共{2}条',
                emptyMsg : "没有符合条件的记录",
                items : [ '-', '&nbsp;&nbsp;', cgcx_pagesize_combo ]
            }
        });
        this.main.add(this.body);
        this.main.doLayout();
        this.cgcxDs.load({
            params: {
                start:0,
                limit:20
            }
        });
    },
    query: function(){
        this.cgcxDs.load( {
            params : {
                start : 0,
                limit : Ext.getCmp('cgcxbbar').pageSize,
                name : Ext.getCmp('cgcxQueryName').getValue(),
                username : Ext.getCmp('cgcxQueryUserName').getValue(),
                fruittypeid : Ext.getCmp('cgcxQueryFruitType').getValue(),
                startTime : dateFormat(Ext.getCmp('cgcxstartdt').getValue()),
                endTime : dateFormat(Ext.getCmp('cgcxenddt').getValue())
            }
        });
    },
    importFromExcel: function(projectid,isagree){
        mainPanel = this;
        var form = new Ext.form.FormPanel({
            labelAlign: 'right',
            bodyStyle: 'padding:5px',
            buttonAlign: 'right',
            frame: true,
            fileUpload: true,
            url:'fruit/import.do',
            labelWidth: 80,
            allowBlank: false,
            msgTarget: 'side',
            defaults: {
                anchor: '90%'
            },
            items: [
                {
                    xtype: 'textfield',
                    name: 'file',
                    allowBlank: false,
                    fieldLabel: '选择文件',
                    inputType: 'file'
                }
            ],
            buttons: [{
                text: '提交',
                scope: this,
                handler: function() {
                    if (form.getForm().isValid()) {
                        form.getForm().submit({
                            waitTitle: '保存数据',
                            waitMsg: '正在保存……',
                            scope: this,
                            method: 'POST',
                            params: {projectid:projectid},
                            submitEmptyText: false,     //禁止提交emptyText
                            success: function(form, action) {
                                Ext.Msg.alert('信息', action.result.info);
                                dlg.close();
                                this.cgcxDs.reload();
                            },
                            failure: function(form, action) {
                                if(action.result.sessionstatus=='timeout'){
                                    alert("出于安全原因，请重新登录！");
                                    //发现请求超时，退出处理代码...
                                    var win = window;
                                    while (win != win.parent){
                                        win = win.parent;
                                    }
                                    win.location = "index.jsp";
                                } else {
                                    Ext.Msg.alert('提示', action.result.info);
                                    dlg.close();
                                    this.cgcxDs.reload();
                                }
                            }
                        });
                    }
                }
            }]
        });
        var dlg = new Ext.Window({
            height: 140,
            width: 500,
            layout: 'fit',
            border: false,
            frame: true,
            title: '只支持excel 2003格式的文件',
            modal: true,
            plain: true,
            resizable: false,
            items: [form],
            listeners: {
                scope: this,
                render: function(fp){
                    form.getForm().waitMsgTarget = fp.getEl();
                },
                show: function(){
                    var f = form.getForm();
                    f.clearInvalid();
                }
            }
        });
        dlg.show();
    }
});


